package com.batchplatform.department.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.batchplatform.common.entity.department.Department;
import com.batchplatform.common.entity.department.DepartmentExample;
import com.batchplatform.common.mapper.department.DepartmentMapper;

@Service
public class DeptService {
	
	@Autowired
	private DepartmentMapper dao;
	
	public void addDepartment(Department dept) {
		dao.insert(dept);
		dept.setCode(dept.getId()+"");
		updateDepartment(dept);
	}
	
	public void updateDepartment(Department dept) {
		dao.updateByPrimaryKey(dept);
	}
	
	public Department getDepartment(int id) {
		return dao.selectByPrimaryKey(id);
	}
	
	public Department getDepartment(String name) {
		DepartmentExample example = new DepartmentExample();
		example.setOrderByClause(name);
		List<Department> list = dao.selectByExample(example);
		if(list == null || list.size() == 0) {
			return new Department();
		}else {
			return list.get(0);
		}
			
	}
	
	public void deleteDepartments(int id) {
		dao.deleteByPrimaryKey(id);
	}
	
	public List<Department> getDepartments() {
		List<Department> list = dao.selectByExample(null);
		if(list == null) {
			return new ArrayList<Department>();
		}
		else {
			return list;
		}
	}
	
	public String getCode(String name) {
		return getDepartment(name).getCode();
	}
	
}
